$(function(){
	//自动加载
	getAdminList('','');
	//添加按钮是否显示
	isCanAdd();
});

//加载列表
function getAdminList(userName, nick) {
	var pageSize = 8;
	
	//默认加载  
	$(document).ready(function() {
		//设置默认第1页
	    init(1);
	});
	//默认加载  
	function init(pageNo){
		//获取用户信息列表
	$("#adminlist").html("");
	$.ajax({
		url: urlcore + "/api/admin/getAdminList?&pageNum="+pageNo+"&pageSize="+pageSize+"&phone="+userName+"&nickName="+nick,
		type: "GET",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				$.each(data.data.list,function(i,n){
					var id = "'"+n.id+"'";
					var userName ="'"+n.userName+"'";
					var nick = "'"+n.nickName+"'";
					var realName = "'"+n.realName+"'";
					var adminlist = 
					'	<tr>'+
					'		<td>'+n.userName+'</td>'+
					'		<td>'+n.nickName+'</td>'+
					'		<td>'+n.realName+'</td>'+
					'		<th>'+new Date(n.gmtDatetime).pattern("yyyy-MM-dd hh:mm:ss")+'</th>'+
					'		<td>'+
					'			<a class="btn btn-primary btn-xs" href="javascript:;" data-toggle="modal" data-target="#add" onclick="updateAdmin('+id+','+userName+','+nick+','+realName+')"> 修改</a>'+
					'			<a class="btn btn-primary btn-xs" href="javascript:;" onclick="updatePassword('+id+')"> 重置密码</a>'+
					'			<a class="btn btn-primary btn-xs" href="javascript:;" onclick="delateAdmin('+id+')"> 删除</a>'+
					'		</td>'+
					'	</tr>';
					$('#adminlist').append(adminlist);
				});
				$("#pager").pager({
						pagenumber: pageNo, 
						pagecount:data.data.pages,
						totalcount:data.data.total,
						buttonClickCallback: PageClick
					}); 
				$('#thiscount').text(data.data.total);
				
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}else{
					$('#thiscount').text(0);
				}
			}

		},
		error:function() {
			alert("error");
		}
	});
	}
	//回调函数  
	PageClick = function(pageclickednumber) {  
	    init(pageclickednumber); 
	}
}

//搜索
function toSearch() {
	var userName = $('#userName').val().trim();
	var nick = $('#nick').val().trim();
	getAdminList(userName,nick);	
}

//删除
function delateAdmin(id) {
	if(confirm("确定要删除吗？")) {
		$.ajax({
			url: urlcore + "/api/admin/deleteAdmin?id="+id,
			type: "POST",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {
					var userName = $('#userName').val().trim();
					var nick = $('#nick').val().trim();
					getAdminList(userName,nick);
				} else if (data.code == 'OVERTIME'){
					var thisUrl = window.location.href;
					if (thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href="login.html";
					}

				} else {
					if (data.msg != '空数据') {
						alert(data.msg)
					}
				}

			},
			error:function() {
				alert("error");
			}
		});
	}
}

//添加/修改
function toAdd() {
	var catalogId="";
	var len = $('input[name="catalog"]:checked').length;
	$('input[name="catalog"]:checked').each(function(i,n){
		if(i == len-1){
   	 	catalogId += $(this).val();
   	 }else{
   	 	catalogId += $(this).val()+",";
   	 }
	});    
	var id = $('#adminId').val().trim();
	var userName = $('#userName_add').val().trim();
	var nick = $('#nick_add').val().trim();
	var password = $('#password_add').val().trim();
	var aginPassword = $('#aginPassword_add').val().trim();
	var realName = $('#realName_add').val().trim();

	if(id != '') {
		$.ajax({
			url: urlcore + "/api/adminAuthority/updateAuthority?userName="+userName+"&nick="+nick+"&realName="+realName+"&id="+id+"&catalog="+catalogId,
			type: "POST",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {
					location.reload();
				} else if (data.code == 'OVERTIME'){
					var thisUrl = window.location.href;
					if (thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href="login.html";
					}

				} else {
					if (data.msg != '空数据') {
						alert(data.msg)
					}
				}

			},
			error:function() {
				alert("error");
			}
		});
		return;
	}

	if (password != aginPassword) {
		alert("两次密码不一致");
		return;
	}
	
	if (userName != '' && password != '') {
		$.ajax({
			url: urlcore + "/api/admin/addAdmin?userName="+userName+"&nick="+nick+"&password="+password+"&realName="+realName+"&catalogIds="+catalogId,
			type: "POST",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {
					location.reload();
				} else if (data.code == 'OVERTIME'){
					var thisUrl = window.location.href;
					if (thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href="login.html";
					}

				} else {
					if (data.msg != '空数据') {
						alert(data.msg)
					}
				}

			},
			error:function() {
				alert("error");
			}
		});
	}else{
		alert("账号/密码不能为空！");
	}
}

//加载修改数据
function updateAdmin(id,userName,nick,realName) {
	thistitle();//获取权限列表
    $('#this_title').html('修改管理员');
	$('#password_add').css("display", "none");
	$('#password_add_label').css("display", "none");
	$('#aginPassword_add').css("display", "none");
	$('#aginPassword_add_label').css("display", "none");	
	
	$('#adminId').val(id);
	$('#userName_add').val(userName);
	$('#nick_add').val(nick);
	$('#realName_add').val(realName);	
	$.ajax({
		url: urlcore + "/api/adminAuthority/getCatalogByAdminId?id=" + id,
		type: "POST",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			var da = data.data;	
			$.each(da,function(i,n){
				$("input:checkbox[value='"+n.catalogId+"']").attr("checked","checked");
			});
			
		},error:function() {
			alert("error");
		}
	})
	

}

//获取所有权限
function thistitle() {
	$('#menu').html("");
	$.ajax({
			url: urlcore + "/api/adminCatalog/getCatalog",
			type: "POST",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				var da = data.data;
				var checkbox = "";
				$.each(da,function(i,n){
					checkbox +='<label><input type="checkbox" name="catalog" value="'+n.id+'">'+n.catalogName+'</label>';
				});
				$('#menu').append(checkbox);
				return data.data;
			},error:function() {
				alert("error");
			}
		});
	$('#password_add').css("display", "block");
	$('#password_add_label').css("display", "block");
	$('#aginPassword_add').css("display", "block");
	$('#aginPassword_add_label').css("display", "block");
	
	$('#this_title').html('添加管理员');
	$('#userName_add').val('');
	$('#password_add').val('');
	$('#aginPassword_add').val('');
	$('#nick_add').val('');
	$('#realName_add').val('');
	$('#adminId').val('');
}

//重置密码
function updatePassword(id) {
	if(confirm("确定要修改密码？")) {
		$.ajax({
			url: urlcore + "/api/admin/updatePassword?adminId="+id,
			type: "POST",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if (data.success == true) {
					var userName = $('#userName').val().trim();
					var nick = $('#nick').val().trim();
					getAdminList(userName,nick);
				} else if (data.code == 'OVERTIME'){
					var thisUrl = window.location.href;
					if (thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href="login.html";
					}

				} else {
					if (data.msg != '空数据') {
						alert(data.msg)
					}
				}

			},
			error:function() {
				alert("error");
			}
		});
	}
}

/**
 * 判断是否能够增加管理员
 */
function isCanAdd(){
	//获取个人信息
	$.ajax({
		url: urlcore + "/api/admin/getadmin",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				if(data.data.roleType == 1){
					$("#addAdmin").show();
				}
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else if(data.code == 'PARAMETER_INVALID') {
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}
			}
			else {
				alert(data.msg);
			}

		},
		error:function() {
			/* Act on the event */
			alert("error");
		}
	});	
	
}
